{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a20a2420",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Polynomial p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
      "Polynomial p2: Poly(x**2 - x - 1, x, modulus=5)\n",
      "Addition result: Poly(2*x**3 - x**2, x, modulus=5)\n",
      "Subtraction result: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
      "Multiplication result: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
      "Modulo operation: Poly(-2*x + 1, x, modulus=5)\n",
      "Quotient: Poly(2*x, x, modulus=5)\n",
      "Greatest common divisor: Poly(x + 2, x, modulus=5)\n",
      "Result of substituting x = 2 into polynomial p1: 1\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, Poly, GF, gcd\n",
    "\n",
    "# Define symbolic variables\n",
    "x = symbols('x')\n",
    "\n",
    "# Define polynomials with coefficients in the ring of integers modulo 5\n",
    "p1 = Poly(2*x**3 + 3*x**2 + x + 1, x, domain=GF(5))\n",
    "p2 = Poly(x**2 + 4*x + 4, x, domain=GF(5))\n",
    "\n",
    "# Print the polynomials\n",
    "print(\"Polynomial p1:\", p1)\n",
    "print(\"Polynomial p2:\", p2)\n",
    "\n",
    "\n",
    "# Polynomial addition\n",
    "add_result = p1 + p2\n",
    "print(\"Addition result:\", add_result)\n",
    "\n",
    "# Polynomial subtraction\n",
    "sub_result = p1 - p2\n",
    "print(\"Subtraction result:\", sub_result)\n",
    "\n",
    "# Polynomial multiplication\n",
    "mul_result = p1 * p2\n",
    "print(\"Multiplication result:\", mul_result)\n",
    "\n",
    "# Note: In the ring of integers modulo n, polynomial division is not always possible\n",
    "# However, modulo operation can be performed\n",
    "mod_result = p1 % p2\n",
    "print(\"Modulo operation:\", mod_result)\n",
    "\n",
    "# Quotient\n",
    "quotient_result = p1 // p2\n",
    "print(\"Quotient:\", quotient_result)\n",
    "\n",
    "# Greatest common divisor\n",
    "gcd_result = gcd(p1, p2)\n",
    "print(\"Greatest common divisor:\", gcd_result)\n",
    "\n",
    "# Substituting values to solve\n",
    "value_result = p1.subs(x, 2)\n",
    "print(\"Result of substituting x = 2 into polynomial p1:\", value_result)\n",
    "\n",
    "## Output Example\n",
    "# Polynomial p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
    "# Polynomial p2: Poly(x**2 - x - 1, x, modulus=5)\n",
    "# Addition result: Poly(2*x**3 - x**2, x, modulus=5)\n",
    "# Subtraction result: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
    "# Multiplication result: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
    "# Modulo operation: Poly(-2*x + 1, x, modulus=5)\n",
    "# Quotient: Poly(2*x, x, modulus=5)\n",
    "# Greatest common divisor: Poly(x + 2, x, modulus=5)\n",
    "# Result of substituting x = 2 into polynomial p1: 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f1b05a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
